package com.example;

import sun.misc.Contended;

/**
 * @ClassName FalseSharing
 * @Description 伪共享
 *              -XX:-RestrictContended
 * @Author: XMK
 * @Date: 2021/4/12 11:07
 * @Version: 1.0
 */
public class FalseSharing {
    private final int SIZE = 100_000_000;

    public static void main(String[] args) throws InterruptedException {
        FalseSharing test = new FalseSharing();
        test.main(new Pointer());
    }

// @Test
    public void main(Pointer P) throws InterruptedException {

        long start = System.nanoTime();

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < SIZE; i++) {
//                P.x++;
                P.x.x++;
            };
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < SIZE; i++) {
//                P.y++;
                P.y.x++;
            };
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        long end = System.nanoTime();

        System.out.println((end - start) / 1000 / 1000);
    }




}

//class Pointer {
//    volatile long x;
//    volatile long y;
//}

//class Pointer {
////    @Contended
//    volatile long x;
////    long p1,p2,p3,p4,p5,p6,p7;
////@Contended
//    volatile long y;
//}
class Pointer {
    MyLong x = new MyLong();
    MyLong y = new MyLong();
}

//@Contended
class MyLong {
    volatile long x;
    long p1,p2,p3,p4,p5;
}
